/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package picshareservertest;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import tcpserver.ServerEvent;

/**
 *
 * @author eric
 */
public class ConfigurationManager extends JFrame {

    private ServerConfiguration serverConfig;
    private JTextField registryField;
    private JTextField storePathField;
    private JTextField portField;
    private JFileChooser chooser;
    private JButton okBtn;
    private List<ServerConfigurationManagerListener> listeners;

    public ConfigurationManager()
    {
       super("File Server Configuration Manager");
       listeners = new ArrayList<ServerConfigurationManagerListener>();
       chooser = new JFileChooser();
       serverConfig = new ServerConfiguration();

       FieldListener fieldChangedListener = new FieldListener();
       okBtn = new JButton("OK");
       okBtn.setEnabled(false);
       okBtn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                serverConfig = new ServerConfiguration();
                serverConfig.setFileStorePath(chooser.getSelectedFile());
                serverConfig.setRegistryServer(registryField.getText());
                boolean valid = false;
                try{
                    serverConfig.setTcpListenerPort(Integer.parseInt(portField.getText()));
                    valid = true;
                }
                catch(NumberFormatException nfe)
                {
                    JOptionPane.showMessageDialog(ConfigurationManager.this, "Listener port must be a valid integer.");
                }

                if (valid)
                   fireConfigurationReceivedEvent(serverConfig);
            }
        });
       JButton cancelBtn = new JButton("Cancel");
       JLabel portLabel = new JLabel("File Transfer Port: ");
       JLabel registryLabel = new JLabel("Registry IP Address: ");
       JLabel storePathLabel = new JLabel("File Store Path: ");
       portField = new JTextField(6);
       portField.getDocument().addDocumentListener(fieldChangedListener);
       registryField = new JTextField(16);
       registryField.getDocument().addDocumentListener(fieldChangedListener);
       storePathField = new JTextField(25);
       storePathField.setEditable(false);
       storePathField.getDocument().addDocumentListener(fieldChangedListener);
       JButton browseButton = new JButton("Browse");

       setLayout(new FlowLayout());

       add(portLabel);
       add(portField);
       add(registryLabel);
       add(registryField);
       add(storePathLabel);
       add(storePathField);
       add(browseButton);
       add(okBtn);
       add(cancelBtn);
       browseButton.addActionListener( new ActionListener(){

            public void actionPerformed(ActionEvent e) {
             
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int option = chooser.showOpenDialog(ConfigurationManager.this);
                if (option == JFileChooser.APPROVE_OPTION)
                {
                    storePathField.setText(chooser.getSelectedFile().getPath());
                }

            }

       });
    }

    public void registerConfigurationListener(ServerConfigurationManagerListener l)
    {
        listeners.add(l);
    }

    private void fireConfigurationReceivedEvent(ServerConfiguration c)
    {
        ServerEvent event = new ServerEvent(this, "New server configuration has been received.");
        for(ServerConfigurationManagerListener l : listeners)
            l.NewConfigurationReceived(event, serverConfig);

    }

    private class FieldListener implements DocumentListener
    {     

        public void insertUpdate(DocumentEvent e) {
           if (registryField.getText() != null && registryField.getText().length() > 0
                   && portField.getText() != null && portField.getText().length() > 0
                   && storePathField.getText() !=null && storePathField.getText().length() > 0)
               okBtn.setEnabled(true);
        }

        public void removeUpdate(DocumentEvent e) {
            if (e.getDocument() != storePathField.getDocument() )
            {
                if (e.getDocument().getLength() < 1)
                {
                   okBtn.setEnabled(false);
                }
            }
        }

        public void changedUpdate(DocumentEvent e) {

        }
    
    }
}
